
//主线程中线程分离
#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;

void* threadRoutine(void* args)
{
    int cnt = 5;
    while(cnt--)
    {
        cout << "thread is running..." << endl;
        sleep(1);
    }
    return nullptr;
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, threadRoutine, nullptr);
    sleep(1);
    pthread_detach(tid);
    int n = pthread_join(tid, nullptr); //等待新线程成功，返回0，等待新线程失败，返回错误码
    cout << "new thread return: " << n << endl;
    return 0;
}